package com.realgodo.site.web.user.config;

import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.providers.encoding.PasswordEncoder;
import org.springframework.security.providers.encoding.ShaPasswordEncoder;
import org.springside.modules.web.struts2.Struts2Utils;

import com.realgodo.site.entity.common.Attachment;
import com.realgodo.site.entity.security.User;
import com.realgodo.site.entity.space.Space;
import com.realgodo.site.service.dic.AttachmentManager;
import com.realgodo.site.web.user.UserDefaultController;

/** 个人系统设置 */
@Result(name = "toindex", type = "redirect", location = "${base}/user/config/")
public class IndexController extends UserDefaultController {

	/***/
	private static final long serialVersionUID = -419637707885810620L;

	@Autowired
	private AttachmentManager attachmentManager;
	private String headPic;
	private String name;// 真实姓名
	private String password;
	private String oldPassword;
	private int isFriendEmail = Space.NEEDLESS_FRIEND_EMAIL;// 好友请求邮件通知
	private int isSoubdNewMessage = Space.NEEDLESS;// 好友请求邮件通知
	/** 是否允许被搜索 */
	private int isAllowBeSearch = Space.NOE_ALLOW_SEARCH;
	/** 信息共享等级 */
	private int publishLevel = Space.SHOW_LEVEL_ANYONE;

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	/** 更新头像 */
	public String head() {
		User user = super.getUser();
		Attachment newhead = new Attachment();
		newhead.setPath(headPic);
		attachmentManager.save(newhead);// 保存新头像
		super.userManager.save(user);
		super.setUser(user);
		return null;
	}

	/** 更新姓名 */
	public String name() {
		User user = super.getUser();
		user.setName(name);
		super.userManager.save(user);
		super.setUser(user);
		return null;
	}

	/** 密码验证 */
	public String checkPassword() {
		if (this.oldPassword == null || "".equals(oldPassword.trim())) {
			Struts2Utils.renderText(Boolean.FALSE.toString());
			return null;
		}
		PasswordEncoder passwordEncoder = new ShaPasswordEncoder();
		if (passwordEncoder.encodePassword(oldPassword.trim(), null).equals(
				getUser().getPassword())) {
			Struts2Utils.renderText(Boolean.TRUE.toString());
		} else {
			Struts2Utils.renderText(Boolean.FALSE.toString());
		}
		return null;
	}

	/** 更新密码 */
	public String password() {
		User user = super.getUser();
		PasswordEncoder passwordEncoder = new ShaPasswordEncoder();
		user.setPassword(passwordEncoder.encodePassword(password, null));
		super.userManager.save(user);
		super.setUser(user);
		return null;
	}

	/** 空间信息设置 */
	public String spaceInfo() {
		Space space = super.getSpace();
		space.setIsFriendEmail(isFriendEmail);
		space.setIsSoubdNewMessage(isSoubdNewMessage);
		space.setPublishLevel(publishLevel);
		space.setIsAllowBeSearch(isAllowBeSearch);
		super.spaceManager.save(space);
		super.setLoginSpace(space);
		return "toindex";
	}

	// --------------get/set------//
	public String getHeadPic() {
		return headPic;
	}

	public void setHeadPic(String headPic) {
		this.headPic = headPic;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public int getIsFriendEmail() {
		return isFriendEmail;
	}

	public void setIsFriendEmail(int isFriendEmail) {
		this.isFriendEmail = isFriendEmail;
	}

	public int getIsSoubdNewMessage() {
		return isSoubdNewMessage;
	}

	public void setIsSoubdNewMessage(int isSoubdNewMessage) {
		this.isSoubdNewMessage = isSoubdNewMessage;
	}

	public int getIsAllowBeSearch() {
		return isAllowBeSearch;
	}

	public void setIsAllowBeSearch(int isAllowBeSearch) {
		this.isAllowBeSearch = isAllowBeSearch;
	}

	public int getPublishLevel() {
		return publishLevel;
	}

	public void setPublishLevel(int publishLevel) {
		this.publishLevel = publishLevel;
	}

}
